package com.gs.djys.checkfragments.license;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gs.djys.Base.MyAppliCation;
import com.gs.djys.R;
import com.gs.djys.Util.NoRecycleView;
import com.gs.djys.Util.NoScroolGridView;
import com.gs.djys.Util.ShowGridAdapter;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.Util.Utils;
import com.gs.djys.adapter.LGRecycleViewAdapter;
import com.gs.djys.adapter.LGViewHolder;
import com.gs.djys.mvp.MVPBaseFragment;
import com.gs.djys.webview.WebViewActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class LicenseFragment extends MVPBaseFragment<LicenseContract.View, LicensePresenter> implements LicenseContract.View {


    @Bind(R.id.xkbh_te)
    TextView xkbhTe;
    @Bind(R.id.hwmc_te)
    TextView hwmcTe;
    @Bind(R.id.txsj_te)
    TextView txsjTe;
    @Bind(R.id.jssj_te)
    TextView jssjTe;
    @Bind(R.id.sys_te)
    TextView sysTe;
    @Bind(R.id.tjs_te)
    TextView tjsTe;
    @Bind(R.id.cfd_te)
    TextView cfdTe;
    @Bind(R.id.mdd_te)
    TextView mddTe;
    @Bind(R.id.hwzl_te)
    TextView hwzlTe;
    @Bind(R.id.chzzl_te)
    TextView chzzlTe;
    @Bind(R.id.zs_te)
    TextView zsTe;
    @Bind(R.id.lts_te)
    TextView ltsTe;
    @Bind(R.id.zj_te)
    TextView zjTe;
    @Bind(R.id.zhfb_te)
    TextView zhfbTe;
    @Bind(R.id.hwwkcc_te)
    TextView hwwkccTe;
    @Bind(R.id.chztwkcc_te)
    TextView chztwkccTe;
    @Bind(R.id.qydjg_te)
    TextView qydjgTe;
    @Bind(R.id.txlx_te)
    TextView txlxTe;
    @Bind(R.id.txlxyh_te)
    TextView txlxyhTe;
    @Bind(R.id.txlx_lay)
    LinearLayout txlxLay;
    @Bind(R.id.listview)
    NoRecycleView listview;
    @Bind(R.id.jckyd_list)
    NoScroolGridView jckydList;
    @Bind(R.id.qzhkyd_list)
    NoScroolGridView qzhkydList;
    @Bind(R.id.chztlkt_list)
    NoScroolGridView chztlktList;
    @Bind(R.id.yzxm_te)
    TextView yzxmTe;
    @Bind(R.id.dljyxkzh_te)
    TextView dljyxkzhTe;
    @Bind(R.id.dljyxkz_list)
    NoScroolGridView dljyxkzList;
    @Bind(R.id.jbrxm_te)
    TextView jbrxmTe;
    @Bind(R.id.jbrsgz_te)
    TextView jbrsgzTe;
    @Bind(R.id.jbrsjh_te)
    TextView jbrsjhTe;
    @Bind(R.id.sfz_list)
    NoScroolGridView sfzList;
    @Bind(R.id.cphm_te)
    TextView cphmTe;
    @Bind(R.id.cpxh_te)
    TextView cpxhTe;
    @Bind(R.id.zbzl_te)
    TextView zbzlTe;
    @Bind(R.id.clxsz_list)
    NoScroolGridView clxszList;
    @Bind(R.id.gchm_te)
    TextView gchmTe;
    @Bind(R.id.cpxh2_te)
    TextView cpxh2Te;
    @Bind(R.id.zbzl2_te)
    TextView zbzl2Te;
    @Bind(R.id.gcxsz_list)
    NoScroolGridView gcxszList;
    @Bind(R.id.activity_disease_detail_scrollview)
    ScrollView activityDiseaseDetailScrollview;
    @Bind(R.id.chztlkt_ms)
    TextView chztlktMs;
    @Bind(R.id.jckyd_te)
    TextView jckydTe;
    @Bind(R.id.qzhkyd_te)
    TextView qzhkydTe;


    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fra_license, container, false);
        ButterKnife.bind(this, view);
        initview();
        return view;
    }

    private void initview() {
        jckydTe.setClickable(false);
        qzhkydTe.setClickable(false);
        chztlktMs.setVisibility(View.GONE);
        mPresenter.getXKXX(MyAppliCation.spUtils.getString("REQ_NO"), "0", getActivity());
        jckydTe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("req_no",MyAppliCation.spUtils.getString("REQ_NO"));
                bundle.putString("sign", "0");
                gotoActivity(WebViewActivity.class,bundle, false);
            }
        });
        qzhkydTe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("req_no",MyAppliCation.spUtils.getString("REQ_NO"));
                bundle.putString("sign", "1");
                gotoActivity(WebViewActivity.class,bundle, false);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void getXKXXs(LicenseBean landBean) {
        if (MyAppliCation.spUtils.getString("REQ_NO").startsWith("GS")) {
            txlxyhTe.setVisibility(View.VISIBLE);
            setTest(txlxyhTe,"通行路线:" , landBean.getData().getRecords().get(0).getROADS());
            setTest(sysTe,"起运地:" , landBean.getData().getRecords().get(0).getSTART_DIST());
            setTest(tjsTe,"途经地区:" , landBean.getData().getRecords().get(0).getPASS_DISTS());
            qydjgTe.setVisibility(View.VISIBLE);
            setTest(qydjgTe,"起运地机构:" , landBean.getData().getRecords().get(0).getORG_NAME());
            if (Utils.isNull(landBean.getData().getRecords().get(0).getVEHICLE_NO1()) && Utils.isNull(landBean.getData().getRecords().get(0).getMODEL1())) {
                //挂车
                gchmTe.setVisibility(View.GONE);
                cpxh2Te.setVisibility(View.GONE);
                zbzl2Te.setVisibility(View.GONE);
            } else {
                //挂车
                setTest(gchmTe,"挂车号码:" , landBean.getData().getRecords().get(0).getVEHICLE_NO1());
                setTest(cpxh2Te,"厂牌型号:" , landBean.getData().getRecords().get(0).getMODEL1());
                setTest(zbzl2Te,"整备质量:" , landBean.getData().getRecords().get(0).getWEIGHT1()+"");
            }
        } else {
            setTest(sysTe,"起运省:" , landBean.getData().getRecords().get(0).getSTART_DIST());
            setTest(tjsTe,"途经省:" , landBean.getData().getRecords().get(0).getPASS_DISTS());
            qydjgTe.setVisibility(View.GONE);
            mPresenter.getRouter(MyAppliCation.spUtils.getString("REQ_NO"));
            //挂车
            setTest(gchmTe,"挂车号码:" , landBean.getData().getRecords().get(0).getVEHICLE_NO1());
            setTest(cpxh2Te,"厂牌型号:" , landBean.getData().getRecords().get(0).getMODEL1());
            setTest(zbzl2Te,"整备质量:" , landBean.getData().getRecords().get(0).getWEIGHT1()+"");
        }
        setTest(xkbhTe,"许可编号:" , landBean.getData().getRecords().get(0).getREQ_NO());
        setTest(hwmcTe,"货物名称:" , landBean.getData().getRecords().get(0).getCARGO_INFO());
        setTest(txsjTe,"通行时间:" , landBean.getData().getRecords().get(0).getSTART_DATE());
        setTest(jssjTe,"结束时间:" , landBean.getData().getRecords().get(0).getEND_DATE());
        setTest(cfdTe,"出发地:" , landBean.getData().getRecords().get(0).getBEGIN_DIST());
        setTest(mddTe,"目的地:" , landBean.getData().getRecords().get(0).getEND_DIST());
        setTest(hwzlTe,"货物质量:" , landBean.getData().getRecords().get(0).getCARGO_WEIGHT());
        setTest(chzzlTe,"车货总质量:" , landBean.getData().getRecords().get(0).getTOTAL_WEIGHT());
        setTest(zsTe,"轴数:" , landBean.getData().getRecords().get(0).getAXLES()+"");
        setTest(ltsTe,"轮胎数:" , landBean.getData().getRecords().get(0).getTYLES()+"");
        setTest(zjTe,"轴距:" , landBean.getData().getRecords().get(0).getWHEELBASES());
        setTest(zhfbTe,"载荷分布:" , landBean.getData().getRecords().get(0).getAXLES_LOAD());
        setTest(hwwkccTe,"货物外廓尺寸:" , landBean.getData().getRecords().get(0).getHWWK());
        setTest(chztwkccTe,"车货总体外廓尺寸:" , landBean.getData().getRecords().get(0).getCHZTWK());
        setTest(yzxmTe,"业主姓名:" , landBean.getData().getRecords().get(0).getNAME());
        setTest(dljyxkzhTe,"道路经营许可证号:" , landBean.getData().getRecords().get(0).getORG_CERT_NO());
        setTest(jbrxmTe,"经办人姓名:" , landBean.getData().getRecords().get(0).getAGENT_NAME());
        setTest(jbrsgzTe,"经办人身份证:" , landBean.getData().getRecords().get(0).getAGENT_ID());
        setTest(jbrsjhTe,"手机号码:" , landBean.getData().getRecords().get(0).getTEL());
//牵引车
        setTest(cphmTe,"车牌号码:" , landBean.getData().getRecords().get(0).getVEHICLE_NO());
        setTest(cpxhTe,"厂牌型号:" , landBean.getData().getRecords().get(0).getMODEL());
        setTest(zbzlTe,"整备质量:" , landBean.getData().getRecords().get(0).getWEIGHT());


        if (landBean.getData().getRecords().get(0).getKY_IMG() != null) {
            ArrayList<String> listImgUrl1 = new ArrayList<>();
            String[] strArr = landBean.getData().getRecords().get(0).getKY_IMG().split(",");
            if (strArr != null && strArr.length > 0) {
                for (int i = 0; i < strArr.length; i++) {
                    listImgUrl1.add(strArr[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter1 = new ShowGridAdapter(getActivity(), listImgUrl1);
            jckydList.setAdapter(ShowGridAdapter1);
        } else {
            setTest(jckydTe,"检测勘验单:" ,"点击查看");
            jckydList.setVisibility(View.GONE);
            jckydTe.setClickable(true);
        }


        if (landBean.getData().getRecords().get(0).getQZ_IMG() != null) {
            ArrayList<String> listImgUrl2 = new ArrayList<>();
            String[] strArr12 = landBean.getData().getRecords().get(0).getQZ_IMG().split(",");
            if (strArr12 != null && strArr12.length > 0) {
                for (int i = 0; i < strArr12.length; i++) {
                    listImgUrl2.add(strArr12[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter2 = new ShowGridAdapter(getActivity(), listImgUrl2);
            qzhkydList.setAdapter(ShowGridAdapter2);
        } else {
            setTest(qzhkydTe,"签字后勘验单:" ,"点击查看");
            qzhkydList.setVisibility(View.GONE);
            qzhkydTe.setClickable(true);
        }



        if (landBean.getData().getRecords().get(0).getTOTAL_IMG() != null) {
            ArrayList<String> listImgUrl3 = new ArrayList<>();
            String[] strArr13 = landBean.getData().getRecords().get(0).getTOTAL_IMG().split(",");
            if (strArr13 != null && strArr13.length > 0) {
                for (int i = 0; i < strArr13.length; i++) {
                    listImgUrl3.add(strArr13[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter3 = new ShowGridAdapter(getActivity(), listImgUrl3);
            chztlktList.setAdapter(ShowGridAdapter3);
        } else {
            chztlktMs.setVisibility(View.VISIBLE);
            chztlktMs.setText(landBean.getData().getRecords().get(0).getTOTAL_IMG_REMARKS());
        }

        if (landBean.getData().getRecords().get(0).getORG_CERT_IMG() != null) {
            ArrayList<String> listImgUrl4 = new ArrayList<>();
            String[] strArr14 = landBean.getData().getRecords().get(0).getORG_CERT_IMG().split(",");
            if (strArr14 != null && strArr14.length > 0) {
                for (int i = 0; i < strArr14.length; i++) {
                    listImgUrl4.add(strArr14[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter4 = new ShowGridAdapter(getActivity(), listImgUrl4);
            dljyxkzList.setAdapter(ShowGridAdapter4);
        }

        if (landBean.getData().getRecords().get(0).getAGENT_ID_IMG() != null) {
            ArrayList<String> listImgUrl5 = new ArrayList<>();
            String[] strArr15 = landBean.getData().getRecords().get(0).getAGENT_ID_IMG().split(",");
            if (strArr15 != null && strArr15.length > 0) {
                for (int i = 0; i < strArr15.length; i++) {
                    listImgUrl5.add(strArr15[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter5 = new ShowGridAdapter(getActivity(), listImgUrl5);
            sfzList.setAdapter(ShowGridAdapter5);
        }

        if (landBean.getData().getRecords().get(0).getDRIVE_IMG() != null) {
            ArrayList<String> listImgUrl6 = new ArrayList<>();
            String[] strArr15 = landBean.getData().getRecords().get(0).getDRIVE_IMG().split(",");
            if (strArr15 != null && strArr15.length > 0) {
                for (int i = 0; i < strArr15.length; i++) {
                    listImgUrl6.add(strArr15[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter6 = new ShowGridAdapter(getActivity(), listImgUrl6);
            clxszList.setAdapter(ShowGridAdapter6);
        }


        if (landBean.getData().getRecords().get(0).getDRIVE_IMG1() != null) {
            ArrayList<String> listImgUrl7 = new ArrayList<>();
            String[] strArr15 = landBean.getData().getRecords().get(0).getDRIVE_IMG1().split(",");
            if (strArr15 != null && strArr15.length > 0) {
                for (int i = 0; i < strArr15.length; i++) {
                    listImgUrl7.add(strArr15[i]);
                }
            }
            ShowGridAdapter ShowGridAdapter7 = new ShowGridAdapter(getActivity(), listImgUrl7);
            gcxszList.setAdapter(ShowGridAdapter7);
        }
    }

    private void setTest(TextView textView,String name ,String data) {
        textView.setText(Html.fromHtml("<font color=\"#666666\">" + name + "</font><font color=\"#5282EB\">" + Utils.replaceNull(data) + "</font>"));

    }

    @Override
    public void getRouters(RouterBean routerBean) {
        if (routerBean.getData() != null) {
            if (routerBean.getData().getRecords() != null && routerBean.getData().getRecords().size() > 0) {
                txlxTe.setVisibility(View.VISIBLE);
                txlxLay.setVisibility(View.VISIBLE);
                LinearLayoutManager manager = new LinearLayoutManager(getContext());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                listview.setLayoutManager(manager);
                LGRecycleViewAdapter<RouterBean.DataBean.RecordsBean> adapter = new LGRecycleViewAdapter<RouterBean.DataBean.RecordsBean>(routerBean.getData().getRecords()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.license_item;
                    }

                    @Override
                    public void convert(LGViewHolder holder, RouterBean.DataBean.RecordsBean zCDBean, int position) {
                        holder.setText(R.id.one, zCDBean.getDIST_NAME());
                        holder.setText(R.id.two, zCDBean.getROUTE_DESC());
                    }
                };
                listview.setAdapter(adapter);
            }
        }
    }
}
